import scrapy
from scrapy import Selector
from Insta.items import InstaItem
import re

class InstaSpider(scrapy.Spider):
    name = "insta"
    allowed_domains = ["insta360.com"]
    # start_urls = ["https://www.insta360.com"]


    def __init__(self):
        self.data = []


    def start_requests(self):
        urls = [
            'https://www.insta360.com',
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response, **kwargs):
        sel = Selector(response)
        insta = InstaItem()
        row = sel.css('#__next > div.main > div.wrap-entry > div ')
        Consumer_product = sel.css('div.index_navItemList__iGNdF.index_expandable__WfWpi > a > img')
        Professional_product = sel.css('div.index_subMenu__DKS3i > div > div.index_navItemList__iGNdF > a > img')
        Icon = sel.css('div.index_navItemList__dRPfo > a > img')

        for style in row:
            style_list1 = style.css('div:nth-child(1)::attr(style)').extract_first()
            style_list2 = style.css('div:nth-child(2)::attr(style)').extract_first()
            background1 = style.css('div:nth-child(1)::attr(data-background-url)').extract_first()
            background2 = style.css('div:nth-child(2)::attr(data-background-url)').extract_first()
            img = style.css('a > img::attr(data-src)').extract_first()
            if style_list1 != None and style_list2 != None:
                self.data.append(re.search(r'url\((.*?)\)', style_list1).group(1))
                self.data.append(re.search(r'url\((.*?)\)', style_list2).group(1))
            elif background1 != None and background2 != None:
                self.data.append(background1)
                self.data.append(background2)
            elif img != None:
                self.data.append(img)
            else:
                print('数据为空')

        for product in Consumer_product:
            img = product.css('::attr(data-scoped-src)').extract_first()
            if img != None:
                self.data.append(img)

        
        for product in Professional_product:
            img = product.css('::attr(data-scoped-src)').extract_first()
            if img != None:
                self.data.append(img)


        for product in Icon:
            img = product.css('::attr(data-scoped-src)').extract_first()
            if img != None:
                self.data.append(img)

        print('当前数据欸：', self.data)
        for i in list(self.data):
            insta['url'] = i
            yield insta
        


